package com.tql.mall.boss.controller.boss;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tql.mall.boss.service.CategoryTwoService;
import com.tql.mall.common.result.R;
import com.tql.mall.model.product.BaseCategory2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(description = "二级分类接口")
@RestController
@RequestMapping("/boss/boss/category2")
public class Category2Controller {
    @Autowired
    private CategoryTwoService categoryTwoService;

    /**
     * 添加二级分类
     * @param
     * @return
     */
    @ApiOperation(value = "保存二级分类")
    @PostMapping("/save")
    public R<Object> saveLevelTwo(@RequestBody BaseCategory2 baseCategory2){
        String msg = categoryTwoService.saveLevelTwo(baseCategory2);
        if (msg == null) return R.ok();
        return R.fail().msg(msg);
    }

    @ApiOperation(value = "删除二级分类")
    @DeleteMapping("/delete/{id}")
    public R<Object> deleteLevelTwo(@PathVariable Long id){
        String msg = categoryTwoService.deleteById(id);
        if (msg == null) return R.ok();
        return R.fail().msg(msg);
    }

    @ApiOperation(value = "编辑二级分类（分类名称以及category1_id）")
    @PutMapping("/edit/{id}/{newName}/{selectedCategory1Id}")
    public R<Object> editLevelTwo(@PathVariable Long id,@PathVariable String newName,@PathVariable Long
                          selectedCategory1Id){
        String msg = categoryTwoService.updateCategory(id,newName,selectedCategory1Id);
        if (msg == null) return R.ok();
        return R.fail().msg(msg);
    }


    @ApiOperation(value = "获取拥有共同category1_id的二级分类列表，传入一级分类id")
    @GetMapping("/getCategoryListWithSameParent/{id}")
    public R<Object> getCategoryListWithSameParent(@PathVariable Long id){
        List<BaseCategory2> list = categoryTwoService.getCategoryListWithSameParent(id);
        return R.ok(list);
    }

    @ApiOperation(value = "获取拥有共同的category1_id的二级分类列表，传入二级分类id")
    @GetMapping("/getCategoryListWithSameCategory1Id/{id}")
    public R<Object> getCategoryListWithSameCategory2Id(@PathVariable Long id){
        List<BaseCategory2> list = categoryTwoService.getCategoryListWithSameCategory1Id(id);
        return R.ok(list);
    }



    @ApiOperation("条件+分页：查询二级分类列表")
    @GetMapping ("/page/{pageNum}/{pageSize}")
    public R<HashMap<String, Object>> getPageInfo(@ApiParam(name = "pageNum",value = "当前页")
                         @PathVariable Integer pageNum,
                         @ApiParam(name = "pageSize",value = "页大小")
                         @PathVariable Integer pageSize,
                         BaseCategory2 baseCategory2Query){
        //获取mp提供的page对象，并且将页号和页大小页传入
        Page<BaseCategory2> page = new Page<>(pageNum,pageSize);
        //设置查询条件,QueryWrapper泛型一定是HospitalSet，因为mp是根据这个泛型所绑定的表生成sql语句的。
        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
        //将可能的查询条件装入queryWrapper
        if(!StringUtils.isEmpty(baseCategory2Query.getCategory1Id())){
            queryWrapper.eq("category1_id",baseCategory2Query.getCategory1Id());
        }
        if (!StringUtils.isEmpty(baseCategory2Query.getName())){
            queryWrapper.like("name",baseCategory2Query.getName());//对分类名称模糊查询
        }
        if (!StringUtils.isEmpty(baseCategory2Query.getId())){
            queryWrapper.eq("id",baseCategory2Query.getId());
        }
        //执行查询
        categoryTwoService.page(page,queryWrapper);
        //返回查询结果(数据、总条数、总页数)
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("total",page.getTotal());
        dataMap.put("list",page.getRecords());
        return R.ok(dataMap);
    }
}
